import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShiroMysqlTest {
    private static final transient Logger log= LoggerFactory.getLogger(ShiroMysqlTest.class);

    public static void main(String[] args) {
        Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro-mysql.ini");
        SecurityManager securityManager=factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        Subject securityUser=SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken("张三","123456");
        token.setRememberMe(true);

        try {
            securityUser.login(token);
            //当我们登录用户后
            log.info("用户{"+securityUser.getPrincipal()+"}登录成功了");
            if (securityUser.hasRole("admin")){
                log.info("has admin");
            }else {
                log.info("not admin");
            }
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }


    }
}
